package com.fordmotor.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fordmotor.config.GaodeConfig;
import com.fordmotor.constant.GaodeEnum;
import com.fordmotor.constant.ResultEnum;
import com.fordmotor.pojo.GaodeObject;
import com.fordmotor.pojo.GaodeResponseVO;
import com.fordmotor.pojo.RequestVO;
import com.fordmotor.service.LbsService;
import com.fordmotor.util.HttpPostUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Primary
@Service
public class LbsServiceImpl implements LbsService {

    private String buildkeyWordSearch(String keyWord){
        StringBuffer gaodeURL = new StringBuffer(GaodeConfig.keyWordSearch);
        gaodeURL.append("key=").append(GaodeConfig.appKey).append("&");
        gaodeURL.append("keywords=").append(keyWord).append("&");
        gaodeURL.append("types=").append(GaodeConfig.sinopec).append("&");
        gaodeURL.append("citylimit=").append(GaodeConfig.citylimit);
        return gaodeURL.toString();
    }

    @Override
    public String keyWordSearch(Double longitude,Double latitude,String keyword){
        String searchUrl = buildkeyWordSearch(keyword);
        String responseData = HttpPostUtil.sendGet(searchUrl);
        GaodeResponseVO gaodeResponseVO = JSONObject.parseObject(responseData, GaodeResponseVO.class);
        return "";
    }

    private String buildAroundSearch(Double longitude,Double latitude,String keyWord){
        StringBuffer gaodeURL = new StringBuffer(GaodeConfig.aroundSearch);
        gaodeURL.append("key=").append(GaodeConfig.appKey).append("&");
        gaodeURL.append("location=").append(longitude+","+latitude).append("&");
        if(GaodeEnum.SINOPEC.getValue().equals(keyWord)) {
            gaodeURL.append("types=").append(GaodeConfig.sinopec).append("&");
        }else if(GaodeEnum.FORD.getValue().equals(keyWord)){
            gaodeURL.append("types=").append(GaodeConfig.ford).append("&");
        }else{
            gaodeURL.append("keywords=").append(keyWord).append("&");
        }
        gaodeURL.append("radius=").append(GaodeConfig.radius).append("&");
        gaodeURL.append("citylimit=").append(GaodeConfig.citylimit);
        return gaodeURL.toString();
    }

    @Override
    public List<GaodeObject> aroundSearch(Double longitude,Double latitude,String keyword){
        String searchUrl = buildAroundSearch(longitude,latitude,keyword);
        String responseData = HttpPostUtil.sendGet(searchUrl);
        GaodeResponseVO gaodeResponseVO = JSONObject.parseObject(responseData, GaodeResponseVO.class);
        if(gaodeResponseVO != null && ResultEnum.SUCCESS.getValue().equals(gaodeResponseVO.getStatus())){
            if(null != gaodeResponseVO.getPois() && gaodeResponseVO.getPois().size() > 0){
               return gaodeResponseVO.getPois();
            }
        }
        return null;
    }

    private String buildIdSearch(String id){
        StringBuffer gaodeURL = new StringBuffer(GaodeConfig.idSearch);
        gaodeURL.append("key=").append(GaodeConfig.appKey).append("&");
        gaodeURL.append("id=").append(id);
        return gaodeURL.toString();
    }

    @Override
    public GaodeObject idSearch(String id) {
        String searchUrl = buildIdSearch(id);
        String responseData = HttpPostUtil.sendGet(searchUrl);
        GaodeResponseVO gaodeResponseVO = JSONObject.parseObject(responseData, GaodeResponseVO.class);
        if(gaodeResponseVO != null && ResultEnum.SUCCESS.getValue().equals(gaodeResponseVO.getStatus())){
            if(null != gaodeResponseVO.getPois() && gaodeResponseVO.getPois().size() > 0){
                return gaodeResponseVO.getPois().get(0);
            }
        }
        return null;
    }
}
